﻿using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApi;
using CommonLib.Core;
using WebApiExtend;
using System.Data.OleDb;
using PddHelper.Models;
using PddHelper.Api;
using Org.BouncyCastle.Ocsp;

namespace WebApi.Controllers
{
    /// <summary>
    /// 基础
    /// </summary>
    /// <typeparam name="T"></typeparam> 
    public class BaseController<T> : BaseController where T : class, new()
    {
        /// <summary>
        /// 构造函数 注入http上下文
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public BaseController(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
        {

        }

        #region  基础操作CURD

        ///// <summary>
        ///// 获取所有-测试接口-测试接口-测试接口-重要的事说3遍
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public virtual List<T> GetList()
        //{
        //    var result = DbContext<T>().ToList();
        //    return result;
        //}

        /// <summary>
        /// 根据主键获取
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public virtual ApiResult<T> GetById([FromBody] dynamic entity)
        {
            var ret = new ApiResult<T>();
            int id = entity.id;
            var result = base.DbContext<T>().GetById(id);
            ret.Success(result);
            return ret;
        }

        /// <summary>
        /// 根据主键删除(逻辑删除)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public virtual ApiResult<int> DeleteById([FromBody] dynamic entity)
        {
            var ret = new ApiResult<int>();
            int id = entity.id;
            var result = DbContext<T>().Delete(id);
            ret.Success(result);
            return ret;
        }

        /// <summary>
        /// 添加返回自增 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public virtual ApiResult<int> Insert([FromBody] T entity)
        {
            var result = DbContext<T>().Insert(entity);
            var ret = new ApiResult<int>();
            ret.Success(result);
            return ret;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        [HttpPost]
        public virtual ApiResult<int> Update([FromBody] T entity)
        {
            var result = DbContext<T>().Update(entity);
            var ret = new ApiResult<int>();
            ret.Success(result);
            return ret;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="IgnoreColumns">忽略的字段</param>
        /// <returns></returns> 
        internal ApiResult<int> Update([FromBody] T entity, List<string>? IgnoreColumns = null)
        {
            var ret = new ApiResult<int>();
            var result = DbContext<T>().Update(entity, IgnoreColumns);
            ret.Success(result);
            return ret;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public virtual object GetListByPage([FromBody] QueryParameters par)
        {
            var result = DbContext<T>().ToPageList(par);
            return result;
        }
        #endregion
    }
}
